package com.my.thread.pool;

import com.google.common.base.Throwables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.BlockingQueue;

/**
 * Created by ZhongGang
 * at 2018/7/13 10:46
 */
public class Worker implements Runnable {
    private static final Logger LOGGER = LoggerFactory.getLogger(Worker.class);

    protected BlockingQueue<Task> tasks;

    Worker(BlockingQueue<Task> tasks) {
        this.tasks = tasks;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Task task = this.tasks.take();
                task.execute();
            } catch (InterruptedException e) {
                LOGGER.error("exception caught, the error is {}", Throwables.getStackTraceAsString(e));
                break;
            } catch (Exception e) {
                LOGGER.error("exception caught, the error is {}", Throwables.getStackTraceAsString(e));
            }
        }
    }
}
